package cn.yuli.controller;

import cn.yuli.commad.Command;
import cn.yuli.commad.impl.NoCommand;
/**
 * 有7个插孔的遥控器
 * @author yuli
 *
 */
public class RemoteController {
	Command[] onCommands;//开启命令
	Command[] offCommands;//关闭命令
	Command undoCommand;//记录最后一个执行的命令,用于撤销
	/**
	 * 初始化遥控器
	 */
	public RemoteController() {
		onCommands = new Command[7];
		offCommands = new Command[7];
		Command noCommand = new NoCommand();//空对象
		for(int i = 0 ; i < 7 ; i++){
			onCommands[i] = noCommand;
			offCommands[i] = noCommand;
		}
		undoCommand = noCommand;
	}
	/**
	 * 为遥控器添加命令
	 * @param slot
	 * @param onCommand
	 * @param offCommand
	 */
	public void setCommand(int slot,Command onCommand,Command offCommand){
		onCommands[slot] = onCommand;
		offCommands[slot] = offCommand;
	}
	/**
	 * 执行打开开关
	 * @param slot 开关位置
	 */
	public void onButtonWasPushed(int slot){
		onCommands[slot].excute();
		undoCommand = onCommands[slot];
	}
	/**
	 * 执行关闭开关
	 * @param slot 开关位置
	 */
	public void offButtonWasPushed(int slot){
		offCommands[slot].excute();
		undoCommand = offCommands[slot];
	}
	/**
	 * 执行撤销最后一个命令
	 */
	public void undoButtonWasPushed(){
		undoCommand.undo();
	}
}
